<?php
class Menu extends AppModel{
    public $useTable  = 'menu';
    public $lang_code = 'vn';
    
    
     public function list_menu($parent_id = 0, $data = NULL) {
        if (!$data) {
            $data = array();
        }
        $lang_code = $this->lang_code;
        $sql = "SELECT menu.*, image.name, menu_desc.title, menu_desc.link
				FROM menu_desc, menu
                LEFT JOIN image ON menu.menu_id = image.post_id AND image.type = 'menu' AND image.`status` = 1
                WHERE 1=1 AND menu.parent_id = $parent_id AND menu.`status` = 1 AND menu.`menu_id` = menu_desc.`menu_id` 
                AND menu_desc.`lang_code` = '$lang_code'
				ORDER BY menu.sort_order";
        $row = $this->query($sql);
        if ($row)
            foreach ($row as $key => $value) {
                $data[$key] = $value;
                $data[$key]['sub_menu'] = $this->list_menu($value['menu']['menu_id']);
            }
        return $data;
    }
    
    public function get_menu_id_by_code($menu_code = null)
    {
        if($menu_code == null)
            return;
        $sql = "SELECT menu_id FROM menu WHERE menu_code = '$menu_code' LIMIT 1";
        $data = $this->query($sql);
        if(!empty($data))
        {
            return $data[0]['menu']['menu_id'];
        }
        else
            return;
    }
    
    
    public function get_menu_by_code($menu_code = null)
    {
        if($menu_code == null)
            return array();
            
        $menu_id = $this->get_menu_id_by_code($menu_code);
        if($menu_id)
        {
            return $this->list_menu($menu_id);
        }
        else
        {
            return array();
        }
    }
    
    
    public function get_menu_by_code2($menu_code = null)
    {
        if($menu_code == null)
            return array();
        $lang_code = $this->lang_code;
        $menu_id = $this->get_menu_id_by_code($menu_code);
        $sql = "SELECT menu.*, image.name, menu_desc.title, menu_desc.link
				FROM menu_desc, menu
                LEFT JOIN image ON menu.menu_id = image.post_id AND image.type = 'menu' AND image.`status` = 1
                WHERE 1=1 AND menu.parent_id = $menu_id AND menu.`status` = 1 AND menu.`menu_id` = menu_desc.`menu_id` 
                AND menu_desc.`lang_code` = '$lang_code'
				ORDER BY menu.sort_order";
        $menu = $this->query($sql);
        return $menu;
    }
    
    function Add($data)
    {
        $this->save($data);
		return $this->getLastInsertId();
    }
    
    public function get_menu($status = "")
    {
        $status = $status == "all" ? '' : 'AND menu.`status` = 1';
    	$lang_code = $this->lang_code;
        $sql = "SELECT menu.*, menu_desc.*, image.`name`
				FROM menu_desc, menu
                LEFT JOIN image ON menu.menu_id = image.post_id AND image.type = 'menu' AND image.`status` = 1
				WHERE menu.menu_id = menu_desc.menu_id $status AND menu_desc.lang_code = '$lang_code' 
				ORDER BY menu.sort_order, menu_desc.title";

		return $this->query($sql);
    }
    
   
    
    
    public function get_menu_by_id($menu_id = "", $lang = '')
    {
    	if($menu_id == "")
    		return null;
    	$lang_code = $this->lang_code;

    	$where_lang = "AND menu_desc.lang_code = '$lang_code'";
    	if($lang == 'all')
    		$where_lang = "";

    	$result = array();
    	$sql = "SELECT * FROM menu WHERE menu_id = '$menu_id'";
    	$data = $this->query($sql);
    	if(!empty($data))
    	{
    		$result['menu'] = $data[0]['menu'];
    	}
    	else
    	{
    		$result['menu'] = array();
    	}
    	

    	$sql = "SELECT * 
				FROM  menu_desc 
				WHERE menu_id = '$menu_id' $where_lang ";
		$data = $this->query($sql);
		$menu_desc = array();
		foreach ($data as $value) {
			$lang_code = $value['menu_desc']['lang_code'];
			$menu_desc[$lang_code] = $value['menu_desc'];

		}
		$result['menu_desc'] = $menu_desc;
		return $result;
    }
    
    function edit($menu_id = "", $data = array())
    {
		return $this->updateAll(
		    $data, 
		    array('menu_id' => $menu_id)
		);
    }
    
    public function get_all_menu($status = "", $parent_id = "")
    {
        $status = $status == "all" ? '' : 'AND menu.`status` = 1';
    	$lang_code = $this->lang_code;
        $parent = $parent_id == "" ? '' : 'AND menu.parent_id = '.$parent_id;
        echo $sql = "SELECT menu.*, menu_desc.title, image.name
				FROM menu_desc, menu
                LEFT JOIN image ON menu.menu_id = image.post_id AND image.type = 'menu' AND image.`status` = 1
                WHERE 1=1 $parent $status AND menu.`menu_id` = menu_desc.`menu_id` 
                AND menu_desc.`lang_code` = '$lang_code'
				ORDER BY menu.sort_order";
		$menu = $this->query($sql);
        $list_menu = array();
        foreach($menu as $value)
        {
            $temp = array();
            $temp = $value['menu'];
            $temp['title'] = $value['menu_desc']['title'];
            $temp['image'] = $value['image']['name'];
            $list_menu[] = $temp;
        }
        return $list_menu;
    }
    
    function filter_menu($arrData, $parent = 0, $level = 0, &$result = array()) {
        if (count($arrData) > 0) {
            foreach ($arrData as $key => $val) {
                if ($parent == $val['parent_id']) {
                    $val['level'] = $level;
                    $result[] = $val;
                    $_parent = $val['menu_id'];
                    unset($arrData[$key]);
                    $this->filter_menu($arrData, $_parent, $level + 1, $result);
                }
            }
        }
        return $result;
    }
    
    function del($menu_id = "")
    {
    	$sql = "DELETE FROM menu WHERE menu_id = '$menu_id'";
    	$this->query($sql);
    	$sql = "DELETE FROM menu_desc WHERE menu_id = '$menu_id'";
    	$this->query($sql);
        $sql = "DELETE FROM image WHERE post_id = '$menu_id' AND type = 'menu'";
    	$this->query($sql);
    	return 1;
    }
    
    function change_status($menu_id = "")
    {
    	$sql = "SELECT * FROM menu WHERE menu_id = '$menu_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $status = $data[0]['menu']['status'];
            $change_status = $status == 1 ? '0' : '1';
            $sql = "UPDATE menu SET status = '$change_status' WHERE menu_id = '$menu_id'";
            $this->query($sql);
            return $change_status;
        }
        return -1;
    }
    
    function valid_menu_code($menu_id = null, $menu_code = null)
    {
        $sql = array(
			"conditions"=> array(
									"menu_code =" => $menu_code,
								)
			);
        $data = $this->find('first',$sql);
        if(empty($data))
            return true;
        else
        {
            $m_id = $data['Menu']['menu_id'];
            if($m_id == $menu_id)
                return true;
            else
                return false;
        } 
    }
    
    function get_parent_menu(){
    	$lang_code = $this->lang_code;
        $sql = "SELECT menu.menu_id, menu_desc.title
                FROM menu
                LEFT JOIN menu_desc ON menu.menu_id = menu_desc.menu_id AND menu_desc.lang_code = '$lang_code'
                WHERE menu.parent_id = 0
                ORDER BY menu_desc.title";
		$menu = $this->query($sql);
        $list_menu = array();
        foreach($menu as $value)
        {
            $list_menu[$value['menu']['menu_id']] = $value['menu_desc']['title'];
        }
        return $list_menu;
    }
    
    
}